package com.erp.module.jiaFang

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import android.widget.AdapterView
import com.erp.module.jiaFang.xGJiaFang.JiaFangKActivity
import com.erp.myAdapter.JiaFangAdapter
import com.erp.bean.JiaFangBean
import com.erp.mclass.UnCeHandler
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import com.erp.myFragment.BaseKFragment
import com.erp.module.jiaFang.addJiaFang.AddJiaFangKActivity
import com.erp.myAdapter.ArrayAdapter
import kotlinx.android.synthetic.main.fragment_jiafang.*
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2018/6/22/022.
 * 作者：刘洋
 * 家纺
 */
class FragmengtJiaFang : BaseKFragment(), PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener, View.OnClickListener, AdapterView.OnItemClickListener {


    private val f = "FragmengtJiaFang"
    var updatedialog: AlertDialog? = null
    lateinit var adapter: JiaFangAdapter
    lateinit var mlist: MutableList<JiaFangBean>
    var page = 0
    var pagesize = 20
    var SouSuoLeiXing = "0"
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_jiafang
    }

    override fun initData() {
        UnCeHandler.buzhou = "initData"
        mlist = ArrayList()
        adapter = JiaFangAdapter(context!!, mlist)
        LvJiaFang.adapter = adapter
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        BtGuoLv.setOnClickListener(this)
        LvJiaFang.onItemClickListener = this
        LlXingJian.setOnClickListener(this)
        val listsp = ArrayList<String>()
        listsp.add("全部")
        listsp.add("出仓单")
        listsp.add("入仓单")
        val adapter = ArrayAdapter(activity, R.layout.lv_sp_tvitem, listsp)
        SpLeiXing.adapter = adapter
//        updatedialog = Myutil.showupdatedialog(activity, "正在获取数据", false)
        Pull.headerRefreshing()
//        httpLieBiao(2)
    }

    fun qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }

    fun httpLieBiao(code: Int) {
        UnCeHandler.buzhou = "httpLieBiao"
        when (SpLeiXing.selectedItem.toString()) {
            "全部" -> SouSuoLeiXing = "0"
            "出仓单" -> SouSuoLeiXing = "-1"
            "入仓单" -> SouSuoLeiXing = "1"
        }
        Httputil.getJiaFangLieBiao(activity, page, pagesize, SouSuoLeiXing, object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                if (data == null) {
                    return
                }
                when (code) {
                    1 -> {
                        mlist.clear()
                        dataJson(data)
                        updatedialog?.dismiss()
                    }
                    2 -> {
                        mlist.clear()
                        Pull.onHeaderRefreshComplete()
                        dataJson(data)
                    }
                    3 -> {
                        Pull.onFooterRefreshComplete()
                        dataJson(data)
                    }
                }
                adapter.notifyDataSetChanged()
//                Log.d("TAAG", "data=" + Myutil.decodeUnicode(data))
            }

            override fun onNot(throwable: String?) {
                when (code) {
                    1 -> {
                        updatedialog?.dismiss()
                    }
                    2 -> {
                        Pull.onHeaderRefreshComplete()
                    }
                    3 -> {
                        Pull.onFooterRefreshComplete()
                    }
                }
//                Log.d("TAAG", "throwable=" + throwable)
            }

        })
    }

    override fun onClick(v: View?) {
        UnCeHandler.buzhou = "onClick"
        when (v) {
            BtGuoLv -> {
                page = 0
                updatedialog = Myutil.showupdatedialog(activity, "正在过滤", false)
                httpLieBiao(1)
            }
            LlXingJian -> {
                addjiafang()
            }
        }
    }

    fun addjiafang() {
        UnCeHandler.buzhou = "addjiafang"
        var c = context
        if(c != null){
            val dialog = AlertDialog.Builder(c)
            dialog.setTitle("提示")
            dialog.setMessage("请选择是出仓还是入仓")
            dialog.setPositiveButton("入仓") { dialog, which ->
                var i = Intent(activity, AddJiaFangKActivity::class.java)
                i.putExtra("code", 1)
                this@FragmengtJiaFang.startActivityForResult(i, 50)
                dialog.dismiss()
            }
            dialog.setNegativeButton("出仓") { dialog, _ ->
                var i = Intent(activity, AddJiaFangKActivity::class.java)
                i.putExtra("code", 2)
                this@FragmengtJiaFang.startActivityForResult(i, 51)
                dialog.dismiss()
            }
            dialog.show()
        }

    }

    override fun onHeaderRefresh(view: PullToRefreshView?) {
        page = 0
        httpLieBiao(2)

    }

    override fun onFooterRefresh(view: PullToRefreshView?) {
        page++
        httpLieBiao(3)

    }

    fun dataJson(data: String) {
        UnCeHandler.buzhou = "dataJson"
        try {
            var js = JSONArray(data)
            for (i in 0 until js.length()) {
                var jo = js[i] as JSONObject
                var bean = JiaFangBean()
                bean.iioo_id = JsonUtil.getvolue(jo, "iioo_id")
                bean.code = JsonUtil.getvolue(jo, "code")
                bean.inum = JsonUtil.getvolue(jo, "inum")
                bean.iw_name = JsonUtil.getvolue(jo, "iw_name")
                bean.o_onum = JsonUtil.getvolue(jo, "o_onum")
                bean.iioo_type = JsonUtil.getvolue(jo, "iioo_type")
                bean.amount = JsonUtil.getvolue(jo, "amount")
                bean.name = JsonUtil.getvolue(jo, "name")
                bean.size = JsonUtil.getvolue(jo, "size")
                bean.auditor_name = JsonUtil.getvolue(jo, "auditor_name")
                bean.audit_time = JsonUtil.getvolue(jo, "audit_time")
                mlist.add(bean)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        UnCeHandler.buzhou = "onActivityResult"
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                49 -> {
                    updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    httpLieBiao(1)
                }
                50 -> {
                    SpLeiXing.setSelection(1)
                    updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    httpLieBiao(1)
                }
                51 -> {
                    SpLeiXing.setSelection(2)
                    updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    httpLieBiao(1)
                }
            }
        }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        var i = Intent(activity, JiaFangKActivity::class.java)
        i.putExtra("id", mlist[position].iioo_id)
        i.putExtra("inum",mlist[position].inum)
        this@FragmengtJiaFang.startActivityForResult(i, 49)
    }

}
//[{"id":"122071","iioo_id":"5359","gi_id":"57417","amount":"5","oid":"210510","batch_id":"3445663","ipoo_id":"0","box_num":null,"iw_name":"饰品仓","inum":"IO-2018-06-00001","iioo_type":"1","audit_time":null,"unit_unit":"个","auditor_name":"","code":"611040371","name":"ZB306-H","size":"170*70*385","enabled":"1","o_onum":"JK8040054","mb_batch":"H18040022"}]
